<?php namespace Phpcmf\Model\Quan; 

// 付款模型示例
class Chuangyejin extends \Phpcmf\Model
{
	public $mytable; // 模块表名称
	public $module; // 模块属性

    private $row;
	// 初始化模型
	public function init($table) {
	    $this->mytable = dr_module_table_prefix($table, SITE_ID);
	    return $this;
	}

    // 付款表单格式
    public function get_myfield() {

        return [
            'fieldtype' => 'Pay', // 字段类型
            'fieldname' => 'fuchiguige',
            'setting' => [
                'option' => [
                    'payfile' => 'chuangyejin.html', // 模板文件
                    'is_finecms' => 1, // 是否启用余额付款
                ],
            ]
        ];
    }

    // 付款类型名称
    public function paytype() {
		$chuangyejindata = \Phpcmf\Service::M()->table('1_quan_form_chuangyejin_shezhi')->where('dirname', 'chuangyejin')->getRow();
		$chuangyejindata['setting'] = dr_string2array($chuangyejindata['setting']);
		$chuangyejinmingcheng = $chuangyejindata['setting']['pages']['chuangyejin_name'];
        return  '<span class="label label-danger"> '.$chuangyejinmingcheng.' </span>'; // 最好2个汉字表述
    }

    // 付款前的权限验证，返回null表示可进行付款，返回字符串是就输出字符串
    // $id 记录id; $paylog 支付表记录数组; $num 数量; $sku 自定义属性
    public function pay_before($id, $num, $sku, $siteid) {
        return '';
    }

    // 付款价格
    // $id 记录id; $num 数量; $sku 自定义属性; $siteid 站点id
    public function get_price($id, $num, $sku, $siteid) {
		
        $cid = (int)\Phpcmf\Service::L('input')->get('cid');
		$table = '1_quan_form_son_data_0';
		if($sku){
			$cid=$sku;
		}
		$skua = \Phpcmf\Service::M()->db->table($table)->where('id', $id)->get()->getRowArray();
		$skus = dr_string2array($skua);
		$skudata = dr_string2array($skus['fuchiguige']);
		//isset($skudata[$cid]);
		//
        // 计算付款价格
        //$data = $this->_get_row($id, $siteid);
		$feiyong = (int)$skudata[$cid][2];
		//dr_debug('文件名称$skudata',$skudata[$cid]);

        return $feiyong; // 测试200
    }

    // 付款数据
    // $id 记录id; $num 数量; $sku 自定义属性; $siteid 站点id
    public function get_row($id, $num, $sku, $siteid) {

        // 查询数据记录，判断是否存在
        $data = $this->_get_row($id, $siteid);
		//dr_debug('$data$data', $data);
		$table = '1_quan_form_son_data_0';
		$skua = \Phpcmf\Service::M()->db->table($table)->where('id', $data['id'])->get()->getRowArray();
		$skus = dr_string2array($skua);
		$skudata = dr_string2array($skus['fuchiguige']);
		if($sku){
			$cid=$sku;
			$feiyong = (int)$skudata[$cid][2];
		} else {
			$feiyong=$this->get_price($id, $num, $sku, $siteid);
		}
		
		//dr_debug('shijianid', $data);
		if ($data['title']) {
			$datatitle = $data['title'];
		} else {
			$datatitle = dr_strcut(dr_clearhtml($data['neirong']), 70);
		}
		\Phpcmf\Service::M('member')->add_money($this->uid, (int)-$feiyong);
        return [
			//'value' => -$feiyong,
            'price' => $this->get_price($id, $num, $sku, $siteid),
            'title' => '参与《'.$datatitle.'》申请',
			'shourutitle' => '活动收入《'.$datatitle.'》',
            'sell_uid' => $data['uid'], // 商家uid
            'sell_username' => $data['author'], // 商家账号
        ];
    }

    // 付款成功
    // $id 记录id; $paylog 支付表记录数组; $num 数量; $sku 自定义属性
    public function success($id, $paylog, $num, $sku) {
        dr_debug('$sku', $sku);
		dr_debug('$paylog', $paylog);
		// 使用正则表达式查找最后一个数字（一个或多个数字）  
		if (preg_match('/-(\d+)$/', $paylog['mid'], $matches)) {  
		    // $matches[1] 将包含最后一个数字  
		    $lastNumber = $matches[1];  
		}
        // 支付成功之后的回调处理动作
        $data = $this->_get_row($id, $paylog['site']);
		

        // 数据
		$tiedata = \Phpcmf\Service::M()->db->table('1_quan_form_son')->where('id', $id)->get()->getRowArray();
		$table = '1_quan_form_son_data_0';
		$skua = \Phpcmf\Service::M()->db->table($table)->where('id', $id)->get()->getRowArray();
		$skus = dr_string2array($skua);
		$skudata = dr_string2array($skus['fuchiguige']);
		$chuangyejin = $skudata[$lastNumber][1];
		$jin= $skudata[$lastNumber][2];
		$feiyong = abs($paylog['value']);
		//dr_debug('paylog', $paylog);
		//减去申请费
		//\Phpcmf\Service::M('member')->add_money($this->uid, (int)-$feiyong);
		$ip = \Phpcmf\Service::L('input')->ip_address();
		//添加正表
		$datas = [
			'cid'=> $tiedata['cid'], //圈子类型
			'catid'=> $tiedata['catid'], //圈子分类类型
			'uid'=> $paylog['uid'], //用户类型
			'author'=> '', //用户类型
			'inputip'=> $ip, //ip类型
			'inputtime'=>SYS_TIME,
			'updatetime'=>SYS_TIME,
			'title'=>$tiedata['title'],
			'status'=>1,
			'tableid'=>0,
			'displayorder'=>0,
			'zhuangtai'=>1,
			'tieziid'=>$id,
			'shenhe'=>1,
			'chuangyejin'=>(int)$chuangyejin,
			'jine'=>$jin,
		];
		\Phpcmf\Service::M()->db->table('1_quan_form_chuangyejin')->insert($datas);
		//添加附表
		$chuangyejinid = \Phpcmf\Service::M()->db->insertID(); // id
		$datafubiao = \Phpcmf\Service::M()->db->table('1_quan_form_chuangyejin')->orderBy('id DESC')->limit(1)->get()->getResultArray();
		$datac = [
			'id'=> $chuangyejinid, //id
			'cid'=> $tiedata['cid'], //类型
			'catid'=> $datafubiao[0]['catid'], //类型
			'uid'=> $paylog['uid'], //用户类型
		];
		
		\Phpcmf\Service::M()->db->table('1_quan_form_chuangyejin_data_0')->insert($datac);
		// 修改会员添加创业金
		
		if($tiedata['fanhaijin']==1){
			$chuangyejinmember = $this->member['chuangyejin']+(int)$jin+(int)$chuangyejin;
		} else {
			$chuangyejinmember = $this->member['chuangyejin']+(int)$jin;
		}
		\Phpcmf\Service::M()->db->table('member_data')->where('id', $paylog['uid'])->update([
		    'chuangyejin' => (int)$chuangyejinmember,
		]);
		//更新统计数量
		\Phpcmf\Service::M()->db->table('1_quan_form_son')->where('id', $id)->update([
		    'fuchishuliang' => (int)$tiedata['fuchishuliang']+1,
		]);
		//名称
		$datamingcheng = \Phpcmf\Service::M()->table('1_quan_form_chuangyejin_shezhi')->where('dirname', 'chuangyejin')->getRow();
		$datamingchengs = dr_string2array($datamingcheng['setting']);
		$datamingchengs['pages']['chuangyejin_name'];
		// 通知给帖主
		$dataim = [
		 'catid'=> 16, //类型
		 'title' => $datamingchengs['pages']['chuangyejin_name'], //标题
		 'zhiding' => '', //置顶
		 'content'=> '申请('.$datamingchengs['pages']['chuangyejin_name'].')'.$datamingchengs['pages']['chuangyejin_name'], //通知内容
		 'uid'=> $paylog['uid'], //发布人的uid
		 'faxinren'=> $paylog['uid'], //发布人的uid
		 'shouxinren'=> (int)$tiedata['uid'],//收信人
		 'tupian'=> '', //图片
		 'shipin'=> '', //视频
		 'yuyin'=> '', //语音
		 'fujian'=> '', //附件
		 'yidu'=> 0, //已读
		 'quanziid'=> $tiedata['cid'], //圈子ID
		 'quantieid'=> $tiedata['id'], //圈帖ID
		 'url' => '',
		];
		\Phpcmf\Service::M('auth')->addtongzhi($dataim);
		
        /*// 打款到收款人账户
        \Phpcmf\Service::M('pay')->add_money($paylog['touid'], $feiyong);
        // 收款人增加一条收入记录
        \Phpcmf\Service::M('pay')->add_paylog([
            'uid' => $paylog['touid'],
            'username' => $paylog['tousername'],
            'touid' => $paylog['uid'],
            'tousername' => $paylog['username'],
            'mid' => $paylog['mid'],
            'title' => $paylog['title'].'收入',
            'value' => $feiyong,
            'type' => $paylog['type'],
            'status' => 1,
            'result' => $paylog['id'],
            'paytime' => SYS_TIME,
            'inputtime' => $paylog['inputtime'],
        ]);
		//添加报名
		\Phpcmf\Service::M()->table($paylog['site'].'_quan_huodong_buy')->insert([
			'quanid' => '',
			'tid' => $id,
			'uid' => $paylog['uid'],
			'touid' => $paylog['touid'],
			'feiyong'=>$feiyong,
			'leixing'=>1,
			'inputtime' => SYS_TIME,
		]);*/
		//更新购买人数统计数量
		/*$jiarutongji = \Phpcmf\Service::M()->db->table($paylog['site'].'_quan_tupian_buy')->where('uid', $this->uid)->where('cid', $id)->countAllResults();
		// 更新记录表
		\Phpcmf\Service::M()->db->table($paylog['site'].'_quan_form_son')->where('id', $id)->update([
			'huodongshuliang' => $jiarutongji,
		]);*/
    }

    // 是否购买了
    public function is_buy($uid, $cid) {
		//dr_debug('$uid', $uid);
        //return \Phpcmf\Service::M()->table(SITE_ID.'_quan_tupian_buy')->where('uid', $uid)->where('cid', $cid)->getRow();
    }

    // 根据id查询表数据
    // $id 记录id;  $siteid 站点id
    private function _get_row($id, $siteid) {

        if (isset($this->row[$id]) && $this->row[$id]) {
            return $this->row[$id];
        }
		
        $this->row[$id] = $this->table($siteid.'_quan_form_son')->get($id);

        return $this->row[$id];
    }


    // 付款成功跳转URL
    // $id 记录id; $paylog 支付表记录数组
    public function call_url($id, $paylog) {
        $data = $this->_get_row($id, $paylog['site']);
        
        $quan = \Phpcmf\Service::M()->table(SITE_ID.'_quan_form_son')->where('id', $data['id'])->getRow();
        $url = "index.php?s=quan&c=son&id=".$quan['id'];
        return dr_url_prefix($url); // 跳转
    }
}